/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zcm.modules.cxf.service;

import java.util.List;

import com.zcm.common.utils.CacheUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zcm.common.persistence.Page;
import com.zcm.common.service.CrudService;
import com.zcm.modules.cxf.entity.CxfSession;
import com.zcm.modules.cxf.dao.CxfSessionDao;

/**
 * ws会话控制Service
 * @author wangwenwei
 * @version 2018-07-28
 */
@Service
@Transactional(readOnly = true)
public class CxfSessionService extends CrudService<CxfSessionDao, CxfSession> {
	public static String CACHE_TYPE_Session = "CxfSessionId";
	public static String CACHE_TYPE_Username = "CxfSessionName";

	public CxfSession get(String id) {
		return super.get(id);
	}

	@Transactional(readOnly = false)
	public void save(CxfSession session) {
		super.save(session);
		// 更新缓存数据
		CacheUtils.put(CACHE_TYPE_Session,session.getSessionId(),session);
		CacheUtils.put(CACHE_TYPE_Username,session.getUser().getLoginName(),session);
	}


	@Transactional(readOnly = false)
	public CxfSession getBySessionId(String sessionId) {
		// 更新缓存数据
		Object obj = CacheUtils.get(CACHE_TYPE_Session,sessionId);
		if(obj==null){
			return null;
		}
		return (CxfSession) obj;
	}


	@Transactional(readOnly = false)
	public CxfSession getByUserName(String userName) {
		// 更新缓存数据
		Object obj = CacheUtils.get(CACHE_TYPE_Username,userName);
		if(obj==null){
			return null;
		}
		return (CxfSession) obj;
	}

	public List<CxfSession> findList(CxfSession cxfSession) {
		return super.findList(cxfSession);
	}
	
	public Page<CxfSession> findPage(Page<CxfSession> page, CxfSession cxfSession) {
		return super.findPage(page, cxfSession);
	}

	@Transactional(readOnly = false)
	public void delete(CxfSession cxfSession) {
		super.delete(cxfSession);
		CacheUtils.remove(CACHE_TYPE_Session,cxfSession.getSessionId());
		CacheUtils.remove(CACHE_TYPE_Username,cxfSession.getUser().getLoginName());
	}
	
}